import { handleActions } from "redux-actions";

import { GET_ORDER, ORDER_SET_CUR_PAGE, ORDER_SET_EACH_PAGE, ORDER_GET_SEARCH } from "../types";
import _ from "lodash";

// 初始值
const initData = {
  curPage: 1,
  eachPage: 5,
  total: 0,
  maxPage: 0,
  rows: []
}

export const order = handleActions({
  [GET_ORDER]: (state, { payload }) => {
    const { total, maxPage, rows } = payload;
    return _.assign({}, state, {
      total,
      maxPage,
      rows
    });
  },
  // 修改当前页
  [ORDER_SET_CUR_PAGE]: (state, { payload }) => {
    const { curPage } = payload;
    const $state = _.clone(state);
    $state.curPage = curPage;
    return $state;
  },
  // 修改每页显示条数
  [ORDER_SET_EACH_PAGE]: (state, { payload }) => {
    const { eachPage } = payload;
    const $state = _.clone(state);
    $state.eachPage = eachPage;
    $state.rows = [];
    return $state;
  },
  // 获取搜索后的数据
  [ORDER_GET_SEARCH]: (state, { payload }) => {
    const { rows } = payload;
    return _.assign({}, state, {
      total: rows.length,
      rows,
    })
  }

}, initData)